﻿--[[
@file   Main top menu control.
@author maxyao
@date   2017-04-19
--]]


offline_main_menu_view = 
{
    menu_id = 0;
    menu_h = 0;
    have_recommend_update = false;

    init = function (parent_id)
        offline_main_menu_view.menu_id = parent_id;
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(parent_id);
        if nil == menu_ptr then
            return;
        end
        menu_ptr:RemoveAll();

        -- 关于我们item
        local new_icon = "ONEIMAGE:client_mainframe\\sys_setting.png?117,119,129,131;Center;0";
        local about_id = offline_main_menu_view.add_nor_item(
            menu_ptr, LoadStr(ID_SYS_MENU_ABOUT), new_icon);
        ui_utility.setproperty_by_id(about_id, "OnClick", "offline_main_menu_view.on_click_about_item");
        ui_utility.set_common_menu_top_item_bkg(about_id);
        offline_main_menu_view.show_recommend_tips(about_id);

        -- 退出item
        local quit_tgp_id = offline_main_menu_view.add_nor_item(
            menu_ptr, LoadStr(ID_SYS_MENU_QUIT_TGP));
        ui_utility.setproperty_by_id(quit_tgp_id, "OnClick", "offline_main_menu_view.on_click_quit_tgp_item");
        ui_utility.set_common_menu_bottom_item_bkg(quit_tgp_id);
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(parent_id);
        if nil ~= menu_ptr then
            local menu_rc = menu_ptr:GetRectEx(TRUE,TRUE);
            offline_main_menu_view.menu_h = menu_rc.bottom - menu_rc.top;
        end
    end;

    show = function (parent_id, is_show, off_set_x, off_set_y)
        offline_main_menu_view.init(parent_id);
        if is_show then
            -- 移动到要显示的位置
            local menu_ptr = ui_utility.get_ctrl_ptr_by_id(parent_id);
            if nil ~= menu_ptr then
                local menu_rc = menu_ptr:GetRectEx(TRUE,TRUE);
                local menu_w = menu_rc.right - menu_rc.left;
                local menu_h = menu_rc.bottom - menu_rc.top;
                menu_rc.left = off_set_x;
                menu_rc.right = menu_rc.left + menu_w;
                menu_rc.top = off_set_y;
                menu_rc.bottom = menu_rc.top;

                menu_ptr:MoveEx(menu_rc,TRUE);
            end
            offline_main_menu_view.show_animation(parent_id);
        else
            offline_main_menu_view.hide_animation(parent_id);
        end
    end;

    do_recommend_update = function()
        offline_main_menu_view.have_recommend_update = true;
        -- offline_main_menu_view.show_recommend_tips();
    end;

    show_recommend_tips = function(about_id)
        local new_id = ui_utility.get_ctrl_id_by_path(about_id, "Ctn_Icon");
        if offline_main_menu_view.have_recommend_update then
            ui_utility.setproperty_by_id(new_id, "Visible", "true");
        else
            ui_utility.setproperty_by_id(new_id, "Visible", "false");
        end
    end;

    add_nor_item = function (menu_ptr, item_name, icon)
        local item_id = 0;
        if nil ~= menu_ptr then
            local const_ui_str = U8toCUIStr("wegame_mainframe\\MenuItem.tpf/TenioUILayout/UIObject/Children/UIObject?btnCfgItem");
            item_id = menu_ptr:AppendItem(const_ui_str);
            ui_utility.setproperty_by_id(item_id, "Caption", item_name);
            local item_icon_id = ui_utility.get_ctrl_id_by_path(item_id, "Ctn_Icon");
            ui_utility.setproperty_by_id(item_icon_id, "Background", icon);
        end
        return item_id;
    end;

    on_click_about_item = function (uilib, control_id)
        event_center.send_event(main_menue_event.MAIN_MENUE_ABOUT, "");
    end;

    on_click_quit_tgp_item = function (uilib, control_id)
        event_center.send_event(main_menue_event.MAIN_MENUE_EXIT_TGP, "");
    end;

    show_animation = function (menu_id)
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(menu_id);
        if nil == menu_ptr then
            return;
        end

        menu_ptr:Show(TRUE);

        local menu_rc = menu_ptr:GetRectEx(TRUE,TRUE);

        -- 开始尺寸
        local resize_start_sz = SIZE:new_local();
        resize_start_sz.cx = menu_rc.right - menu_rc.left;
        resize_start_sz.cy = 0;

        -- 展开后的尺寸
        local resize_end_sz = SIZE:new_local();
        resize_end_sz.cx = menu_rc.right - menu_rc.left;
        resize_end_sz.cy = offline_main_menu_view.menu_h;


        -- 缩放动画
        animation_utility.init(
            offline_main_menu_view.menu_id,
            "Setting_menu_Resize", animation_utility.ANI_TYPE.ANI_SIZE);
        animation_utility.size_animation(
            offline_main_menu_view.menu_id, resize_start_sz, resize_end_sz, 1, 0);

    end;

    hide_animation = function (menu_id)
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(menu_id);
        if nil ~= menu_ptr then
            menu_ptr:Show(FALSE);
        end
    end;
};